package com.ysd.twocrm.quartz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysd.twocrm.entity.Userchecks;
import com.ysd.twocrm.mapper.UserChecksMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
@EnableScheduling//开启定时任务
public class UsersCheckInQuartz {
@Autowired
    private UserChecksMapper userChecksMapper;
    /*
     * @Description 这个方法首先获取当天打卡并且根据当天的时间来查询全部已经打卡但是没有签退下班的人，
     * 然后判断当前的下班字段是否为空，如果为空，就将当前的打卡下班修改成当天下午5点
     * 这个方法会在美团23点59分0秒执行
     * @param
     * @return void
     * @author liusupeng
     * @date 2020/5/24 0:08
     */
@Scheduled(cron = "0 59 23 * * * ")//每天23点0分0秒执行
public void exitcheckout() throws ParseException {

    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String dateout=sdf.format(new Date())+" 17:00:00";
    QueryWrapper<Userchecks> queryWrapper=new QueryWrapper<Userchecks>();
    queryWrapper.likeRight("userCheckInTime",sdf.format(new Date())).eq("userCheckStatus",1);
    List<Userchecks> userchecks = userChecksMapper.selectList(queryWrapper);
    for(int i=0;i<userchecks.size();i++){
        if(userchecks.get(i).getUserCheckOutTime()==null){

            Userchecks uc=new Userchecks();
            uc.setUserCheckId(userchecks.get(i).getUserCheckId());
            uc.setUserCheckOutTime(sdf1.parse(dateout));
            userChecksMapper.updateById(uc);

        }
    }
}
}
